package com.zz.load;

import com.zz.load.exception.MyServletException;
import com.zz.load.servlet.MyServlet;
import org.junit.Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class TestMain{
    OutputStream outputStream;
    InputStream inputStream;
    ConcurrentHashMap<String, Object> pathMap = new ConcurrentHashMap<>();
    Map<String, String> kvMap = new HashMap<>();
    String method;
    String path;


    @Test
    public void ma1() throws Exception{
        Properties pro = new Properties();
        pro.load(new FileInputStream("web.properties"));
        String property = pro.getProperty("/zhangzhe");
        System.out.println("property = " + property);
    }

    @Test
    public void ma2() throws Exception{
        ServerSocket serverSocket = new ServerSocket(9090);
        while (!serverSocket.isClosed()){
            Socket accept = serverSocket.accept();
            inputStream = accept.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            String firstStr = br.readLine();
            System.out.println(firstStr);
//            GET / HTTP/1.1
//            GET /login?username=zhangzhe&password=123456 HTTP/1.1
            outputStream = accept.getOutputStream();
            ma3();
        }
    }

    @Test
    public void ma3() throws Exception{
        PrintWriter pw = new PrintWriter(outputStream);
        String respHeader = "HTTP/1.1 200 OK\r\n" +
                "Content-Type: text/html;charset=utf-8\r\n\r\n";

        String resp = respHeader + "<h1>你好啊，欢迎访问！！！</h1>";
        pw.println(resp);
        pw.flush();
    }

    @Test
    public void ma4() throws Exception{
        Properties pro = new Properties();
        pro.load(new FileInputStream("web.properties"));
        Set<String> stringSet = pro.stringPropertyNames();
        for (String key : stringSet) {
            String value = pro.getProperty(key);
//            pathMap.put(key, value);
            System.out.println(key + " -> " + value);
        }
    }

    @Test
    public void ma5() throws Exception{
        //GET /login?username=zhangzhe&password=123456 HTTP/1.1
        //GET /login HTTP/1.1
        String str = "GET /login HTTP/1.1";
        String[] arr = str.split(" ");
        method = arr[0];
        if(arr[1].indexOf("?") != -1){
            path = arr[1].substring(0, arr[1].indexOf("?"));
            String[] resourceArr = arr[1].substring(arr[1].indexOf("?")+1).split("&");
            for (String resource : resourceArr) {
                String[] kv = resource.split("=");
                kvMap.put(kv[0], kv[1]);
            }

            Set<String> stringSet = kvMap.keySet();
            for (String key : stringSet) {
                System.out.println(key + " -> " + kvMap.get(key));
            }
        }else{
            path = arr[1].substring(0);
            System.out.println("path = " + path);
        }
    }

    @Test
    public void ma6() throws Exception{
        Properties pro = new Properties();
        pro.load(new FileInputStream("web.properties"));
        Set<String> stringSet = pro.stringPropertyNames();
        for (String key : stringSet) {
            String value = pro.getProperty(key);
            Object obj = Class.forName(value).newInstance();
            if(obj instanceof MyServlet){
                pathMap.put(key, obj);
            }else{
                throw new MyServletException("404 NOT Found");
            }
            System.out.println(key + " -> " + value);
        }
    }

    @Test
    public void ma7() throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("index.html")));
        String buf = null;
        StringBuilder sb = new StringBuilder();
        while ((buf = br.readLine()) != null){
            sb.append(buf);
        }
        System.out.println(sb.toString());
    }
}